package thread;

public class SyncDemo2 {
    public static void main(String[] args) {
        shop s = new shop();
        shop s1 = new shop();
        shop s2 = new shop();
        Thread t1 = new Thread("张麒") {
            public void run() {
                s1.buy();
            }
        };
        Thread t2 = new Thread("狗") {
            public void run() {
                s2.buy();
            }
        };
        t1.start();
        t2.start();
    }
}
/*class shop{
    public void buy(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"正在买衣服");
            Thread.sleep(5000);
            System.out.println(t.getName()+"正在试衣服");
            Thread.sleep(5000);
            System.out.println(t.getName()+"结账");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}*/
class shop{
    public void buy(){
        try {
            int a = 123;
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"正在买衣服");
            Thread.sleep(5000);
            synchronized ("123"){
                System.out.println(t.getName()+"正在试衣服");
                Thread.sleep(5000);
            }
            System.out.println(t.getName()+"结账");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
